package juc.topic;

/**
 * 两个线程交替打印出a1b2c3.....z26
 */
public class AlternatePrintingNumberLetter {
    private static final Object lock = new Object();

    private static boolean printNumber = true;


    public static void main(String[] args) {

        new Thread(() -> {
            for (int i = 1; i <= 26; i++) {
                synchronized (lock) {
                    while (!printNumber) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print(i);
                    printNumber = false;
                    lock.notify();
                }
            }
        },"数字线程").start();


        new Thread(() -> {
            for (char i = 'a'; i <= 'z'; i++) {
                synchronized (lock) {
                    while (printNumber) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print(i);
                    printNumber = true;
                    lock.notify();
                }
            }
        },"字母线程").start();
    }
}
